// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { Item } from '../object/Item';

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemGrid extends cc.Component {
  private sprite: cc.Sprite;
  public currentItem: Item = null;

  onLoad() {
    this.sprite = this.getComponent(cc.Sprite);
  }

  setItem(item: Item | null) {
    if (!this.sprite) {
      return;
    }
    if (item === this.currentItem) {
      return;
    }
    this.currentItem = item;
    if (!item) {
      this.sprite.spriteFrame = null;
    } else {
      if (!item.spriteFramePromise) {
        this.sprite.spriteFrame = item.spriteFrame;
      } else {
        item.spriteFramePromise.then((res) => {
          this.sprite.spriteFrame = res;
        });
      }
    }
  }
}
